/**
 * 
 */
package ltu.m7011e.client.view;

import java.util.ArrayList;
import java.util.List;

import ltu.m7011e.client.presenter.MainPresenter;
import ltu.m7011e.shared.SharedConstants;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockLayoutPanel.Direction;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author robinelmouahidine
 *
 */
public class MainView extends Composite implements MainPresenter.Display {

	/**
	 * An instance of the Shared Constants.
	 */
	private final SharedConstants constants = GWT.create(SharedConstants.class);

	public static Integer POSITION_OF_TOPLINK_TAB = 0;
	public static Integer POSITION_OF_NEWESTLINK_TAB = 1;
	public static Integer POSITION_OF_CATEGORY_TAB = 2;
	
	// Element of HeaderPanel
	private Button loginButton;
	private Button homeButton;
	private Label userInfo;

	private TabPanel mainPanel;
	// TODO : Fix the postButton x3 ...
	private Button postTopButton;
	private Button postNewestButton;
	private Button postCategoryButton;
	private HorizontalPanel radioCategoryPanel;
	private FlexTable newestLinkTable;
	private FlexTable topLinkTable;
	private FlexTable catLinkTable;
	private Label errorLabel;

	public void setData(List<String> data) {
		// Fill the view with data  - unused
	}

	public MainView() {
		
		// Initialization of the VerticalPanel that split the header and the view content
		VerticalPanel splitPanel = new VerticalPanel();
		initWidget(splitPanel);
		splitPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		
		// Header panel which possibly can allow navigation 
		HorizontalPanel headerPanel = new HorizontalPanel();
		//headerPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		//headerPanel.setStyleName("headerPanel");
		headerPanel.setSpacing(10);
		loginButton = new Button("Login using google account"); 
		userInfo = new Label("Anonymous user");
		homeButton = new Button("Home");
		
		userInfo.setStyleName("userInfo");
		headerPanel.add(userInfo);
		headerPanel.add(loginButton);
		headerPanel.add(homeButton);
		headerPanel.setStyleName("headerPanel");
		
		// Add the header panel to the mainView
		splitPanel.add(headerPanel);

		// Initializing the TabLayoutPanel
		mainPanel = new TabPanel();
		//initWidget(mainPanel);
		mainPanel.setAnimationEnabled(true);
		mainPanel.getElement().getStyle().setMarginBottom(10.0, Unit.PX);
		mainPanel.setWidth("380");
		mainPanel.setStyleName("mainPanel");
		
		
		// Load tab Title 
		String[] tabTitles = constants.cMainTabTitle();

		DecoratorPanel topLinkTableDecorator = new DecoratorPanel();
		topLinkTableDecorator.setWidth("100%");
		topLinkTableDecorator.setWidth("18em");
		
		DecoratorPanel newestLinkTableDecorator = new DecoratorPanel();
		topLinkTableDecorator.setWidth("100%");
		topLinkTableDecorator.setWidth("18em");

		DecoratorPanel catLinkTableDecorator = new DecoratorPanel();
		topLinkTableDecorator.setWidth("100%");
		topLinkTableDecorator.setWidth("18em");
		
		// Initialize the FlexTabel
		topLinkTable = new FlexTable();
		errorLabel = new Label();
		postTopButton = new Button("Post a new Link");

		FlexCellFormatter topLinkCellFormatter = topLinkTable.getFlexCellFormatter();
		topLinkTable.addStyleName("cw-FlexTable");
		topLinkTable.setWidth("50em");
		topLinkTable.setCellSpacing(3);

		// Add text & postButton
		topLinkCellFormatter.setHorizontalAlignment(
				0, 1, HasHorizontalAlignment.ALIGN_LEFT);
		topLinkTable.setHTML(0, 0, "<h2>Top Links ... loading ...<h2>");
		topLinkTable.setWidget(0, 1, postTopButton);
		postTopButton.addStyleName("postButton");
		
		// Initialize the newestLinkTab
		newestLinkTable = new FlexTable();
		postNewestButton = new Button("Post a new Link");

		FlexCellFormatter newestLinkCellFormatter = newestLinkTable.getFlexCellFormatter();
		newestLinkTable.addStyleName("cw-FlexTable");
		newestLinkTable.setWidth("50em");
		newestLinkTable.setCellSpacing(3);

		// Add text & postButton
		newestLinkCellFormatter.setHorizontalAlignment(
				0, 1, HasHorizontalAlignment.ALIGN_LEFT);
		newestLinkTable.setHTML(0, 0, "<h2>Most recent links ... loading ...<h2>");
		newestLinkTable.setWidget(0, 1, postNewestButton);
		postNewestButton.addStyleName("postButton");


		// Initialize the catLinkTable
		catLinkTable = new FlexTable();
		postCategoryButton = new Button("Post a new Link");

		FlexCellFormatter cattLinkCellFormatter = catLinkTable.getFlexCellFormatter();
		catLinkTable.addStyleName("cw-FlexTable");
		catLinkTable.setWidth("50em");
		catLinkTable.setCellSpacing(3);

		// Add text & postButton
		VerticalPanel categoryChoicePanel = new VerticalPanel();
		cattLinkCellFormatter.setHorizontalAlignment(
				0, 1, HasHorizontalAlignment.ALIGN_LEFT);
		categoryChoicePanel.add(new HTML("<h2>Newest Links by category :<h2>"));
		radioCategoryPanel = this.generateRadioPanel();
		categoryChoicePanel.add(radioCategoryPanel);

		catLinkTable.setWidget(0, 0, categoryChoicePanel);
		catLinkTable.setWidget(0, 1, postCategoryButton);
		postCategoryButton.addStyleName("postButton");
		
		// Add the Top Link tab
		topLinkTableDecorator.add(topLinkTable);
		mainPanel.add(topLinkTableDecorator, tabTitles[0]);
		// Add Newest Link Tab
		newestLinkTableDecorator.add(newestLinkTable);
		mainPanel.add(newestLinkTableDecorator, tabTitles[1]);
		// Add a Category Tab
		catLinkTableDecorator.add(catLinkTable);
		mainPanel.add(catLinkTableDecorator, tabTitles[2]);


		// Select the tab to display
		mainPanel.selectTab(0);
		homeButton.setEnabled(false);
		
		// Add the maintab to the vertical content zone
		splitPanel.add(mainPanel);
		}

	public HasClickHandlers getPostTopButton() {
		return postTopButton;
	}
	public HasClickHandlers getPostCategoryButton() {
		return postCategoryButton;
	}
	public HasClickHandlers getPostNewestButton() {
		return postNewestButton;
	}
	
	public Button getLoginButton() {
		return loginButton;
	}
	
	public Label getUserInfo() {
		return userInfo;
	}

	public void displayTab(Integer tabNumber) {
		this.mainPanel.selectTab(tabNumber);
	}

	public FlexTable getTopLinkTable() {
		return topLinkTable;
	}
	
	public FlexTable getNewestLinkTable() {
		return newestLinkTable;
	}
	
	public FlexTable getCategoryLinkTable() {
		return catLinkTable;
	}

	public HasSelectionHandlers<Integer> getMainPanel(){
		return mainPanel;
	}

	/**
	 * Get the CheckBox button from the radioCategoryPanel and send back their HasClickHandlers
	 * in order to implement the Handler to fetch new links
	 * @return
	 */
	public ArrayList<HasClickHandlers> getRadioButton(){
		ArrayList<HasClickHandlers> myList = new ArrayList<HasClickHandlers>();
		for (int i=0; i < radioCategoryPanel.getWidgetCount(); i++) {
			if ( radioCategoryPanel.getWidget(i) instanceof RadioButton ){
				// Quite dirty ^
				myList.add((RadioButton) radioCategoryPanel.getWidget(i));
			}
		}
		return myList;
	}

	/**
	 * Generate the checkBox button for each categories of link and insert them into a HorizontalPanel
	 * (The categories are loaded from the Constants properties in shared/
	 * @return HorizontalPanel with checkBox button
	 */
	private HorizontalPanel generateRadioPanel(){
		int i = 0;
		final HorizontalPanel radioPanel = new HorizontalPanel();
		String[] availaibleCats = constants.cLinkAvailableCategories();
		for (String category : availaibleCats) {
			if ( i == 0 ){
				RadioButton firstChecked = new RadioButton("Categories",category);	
				firstChecked.setValue(true);
				i++;
				radioPanel.add(firstChecked);
			}else {
				radioPanel.add(new RadioButton("Categories",category));	
			}
		}
		return radioPanel;
	}

	public Widget asWidget() {
		return this;
	}

}
